package com.hw.hcit.feature;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JTestItem;

public class KeypadTest extends JTestItem implements OnClickListener 
{
	private TextView vUp;
	private TextView vDown;
	private TextView power,result;
	private int TEST_KEY_CNT;
	private boolean keyPressed[];
	private boolean ignoreFirstClick;
	private Context mContext;
	private String TAG = "KeypadTest";
	private Button mButton1,mButton2;
	
	 private Handler mHandler = new Handler() {
			public void handleMessage(Message msg) {
				
				switch (msg.what) {
				case 0:
					mButton1.setEnabled(true);
					result.setVisibility(View.VISIBLE);
					break;
			
				default:
					break;
				}
			}
		};

	public KeypadTest() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
			View rootView = inflater.inflate(R.layout.keypad_test, container, false);
			power = (TextView) rootView.findViewById(R.id.power);
			vUp = (TextView) rootView.findViewById(R.id.vup);
			vDown = (TextView) rootView.findViewById(R.id.vdown);
			result = (TextView) rootView.findViewById(R.id.test_result);
			mButton1 = (Button) rootView.findViewById(R.id.button1);
			mButton2 = (Button) rootView.findViewById(R.id.button2);
			mButton1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if(Config.CURR_PROJECT.isAuto()){
						if (!Config.ageingTested) {
							rewriteOneByteOfNv2499(2, 'P');

						} else {
							rewriteOneByteOfNv2499(33, 'P');
						}
					}
					 testFinish(true, 1, true);
					
				}
			});
			mButton2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if(Config.CURR_PROJECT.isAuto()){
						if (!Config.ageingTested) {
							rewriteOneByteOfNv2499(2, 'F');

						} else {
							rewriteOneByteOfNv2499(33, 'F');
						}
					}
					testFinish(false, 1, false);
					
				}
			});
			
			TEST_KEY_CNT = 3;
			keyPressed = new boolean[TEST_KEY_CNT];
			keyPressed[0] = keyPressed[1] = keyPressed[2] = false;
			return rootView;
	}

	@Override
	public boolean getResult() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getResultInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startTest() {
		// TODO Auto-generated method stub
		//start to test
	}

	@Override
	public void stopTest() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void refresh()
	{
		
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		Log.d(TAG, "KeypadTest onAttach");
		super.onAttach(activity);
		//activity.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
		mContext = activity;
		ignoreFirstClick = true;
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		Log.d(TAG, "KeypadTest onResume");
		super.onResume();
        mButton1.setEnabled(false);
//        mButton2.setEnabled(false);
		Settings.Global.putInt(mContext.getContentResolver(), "keypad_test_running", 1);
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		Log.d(TAG, "KeypadTest onPause");
		super.onPause();
		Settings.Global.putInt(mContext.getContentResolver(), "keypad_test_running", 0);
		ignoreFirstClick = false;
	}

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//nv write
		//Config.curr_pro.isAuto()
		//Config.curr_pro.getType()
		//Config.CURR_PROJECT.getType()
	}
	
	 @Override
	public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
		 /*gaoshenpan 2015-08-12 fix first keyenent can not respond begin*/
		/*if (Config.CURR_PROJECT.isAuto()) {
			if (ignoreFirstClick) {
				ignoreFirstClick = false;
				return true;
			}
		}*/
		 /*gaoshenpan 2015-08-12 fix first keyenent can not respond end*/
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_VOLUME_UP:
			if (keyPressed[0]) {
				vUp.setTextColor(Color.GRAY);
				keyPressed[0] = false;
			} else {
				vUp.setTextColor(Color.GREEN);
				keyPressed[0] = true;
			}
			break;
		case KeyEvent.KEYCODE_VOLUME_DOWN:
			if (keyPressed[1]) {
				vDown.setTextColor(Color.GRAY);
				keyPressed[1] = false;
			} else {
				vDown.setTextColor(Color.GREEN);
				keyPressed[1] = true;
			}
			break;
		case KeyEvent.KEYCODE_POWER:
			if (keyPressed[2]) {
				power.setTextColor(Color.GRAY);
				keyPressed[2] = false;
			} else {
			    power.setTextColor(Color.GREEN);
			    keyPressed[2] = true;
			}
			break;
		}
		
		if (keyPressed[0] && keyPressed[1] && keyPressed[2]) {
			Log.d(TAG, "KeypadTest testFinish");
			mHandler.sendEmptyMessage(0);
		}
        return true;
    }

}
